import { mockAPI } from './mockData'

// 权限检查工具
export class PermissionManager {
  static async checkPermission(userId, permission) {
    try {
      const result = await mockAPI.checkPermission(userId, permission)
      return result.success && result.data.hasPermission
    } catch (error) {
      console.error('权限检查失败:', error)
      return false
    }
  }

  static async getUserRole(userId) {
    try {
      const result = await mockAPI.getUserRole(userId)
      return result.success ? result.data : null
    } catch (error) {
      console.error('获取用户角色失败:', error)
      return null
    }
  }

  // 检查当前用户是否有指定权限
  static async hasPermission(permission) {
    const userStr = localStorage.getItem('user')
    if (!userStr) return false
    
    const user = JSON.parse(userStr)
    return await this.checkPermission(user.id, permission)
  }

  // 检查当前用户角色
  static getCurrentUserRole() {
    const userStr = localStorage.getItem('user')
    if (!userStr) return null
    
    const user = JSON.parse(userStr)
    return user.role
  }

  // 检查是否为管理员
  static isAdmin() {
    return this.getCurrentUserRole() === 'admin'
  }

  // 检查是否为医生
  static isDoctor() {
    return this.getCurrentUserRole() === 'doctor'
  }

  // 检查是否为患者
  static isPatient() {
    return this.getCurrentUserRole() === 'patient'
  }

  // 权限常量
  static PERMISSIONS = {
    // 患者权限
    VIEW_APPOINTMENTS: 'view_appointments',
    CREATE_APPOINTMENTS: 'create_appointments',
    CANCEL_APPOINTMENTS: 'cancel_appointments',
    
    // 医生权限
    MANAGE_APPOINTMENTS: 'manage_appointments',
    VIEW_PATIENTS: 'view_patients',
    MANAGE_SCHEDULE: 'manage_schedule',
    
    // 管理员权限
    VIEW_ALL: 'view_all',
    MANAGE_USERS: 'manage_users',
    MANAGE_DOCTORS: 'manage_doctors',
    MANAGE_DEPARTMENTS: 'manage_departments',
    VIEW_STATISTICS: 'view_statistics',
    MANAGE_SYSTEM: 'manage_system'
  }

  // 角色常量
  static ROLES = {
    ADMIN: 'admin',
    DOCTOR: 'doctor',
    PATIENT: 'patient'
  }
}

// 权限指令
export const permissionDirective = {
  mounted(el, binding) {
    const { value } = binding
    const userRole = PermissionManager.getCurrentUserRole()
    
    if (value && value.roles && !value.roles.includes(userRole)) {
      el.style.display = 'none'
    }
    
    if (value && value.permissions) {
      // 异步检查权限
      PermissionManager.hasPermission(value.permissions).then(hasPermission => {
        if (!hasPermission) {
          el.style.display = 'none'
        }
      })
    }
  }
}

// 权限组件
export const Permission = {
  name: 'Permission',
  props: {
    roles: {
      type: Array,
      default: () => []
    },
    permissions: {
      type: [String, Array],
      default: ''
    }
  },
  setup(props, { slots }) {
    const userRole = PermissionManager.getCurrentUserRole()
    const hasRole = props.roles.length === 0 || props.roles.includes(userRole)
    
    if (!hasRole) {
      return () => null
    }
    
    if (props.permissions) {
      const permissions = Array.isArray(props.permissions) ? props.permissions : [props.permissions]
      
      return () => {
        // 这里可以添加异步权限检查逻辑
        return slots.default ? slots.default() : null
      }
    }
    
    return () => slots.default ? slots.default() : null
  }
} 